angular.module('Controller')
.controller('QgyPickContractModalCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
#    $scope.type=type=items.type
#    $scope.data=items.data
    $scope.modalTitle='选择合同'
    $scope.modalEnableList=commonConstant.enabledList()
    initSearchData = ()->
        $scope.search = {
#            type:undefined ,
            apartmentId:undefined,
            houseNo:undefined,
            customerName:undefined,
            customerPhone:undefined
        }
    initSearchData()
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.appointmentStatusList=commonConstant.appointmentStatusList(true)
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count(),
#            type:$scope.search.type,
            status:1,
            apartmentId:$scope.search.apartmentId,
            houseNo:$scope.search.houseNo,
            customerName:$scope.search.customerName,
            customerPhone:$scope.search.customerPhone
        }
        $http.get('/contract',params:requestData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        chooseItem:(item)->
            $scope.pickedItem=angular.copy(item)
    $scope.confirm = ()->
        $uibModalInstance.close($scope.pickedItem)
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)